package com.dlc.shop.api.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.allinpay.service.AllinpayService;
import com.dlc.shop.bean.dto.allinpay.*;
import com.dlc.shop.common.allinpay.constant.AllinpayConstant;
import com.dlc.shop.common.allinpay.order.resp.*;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.security.common.util.AuthUserContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author gaozijie
 * @date 2023-03-28
 */
@Tag(name = "通联支付-订单接口(用户端)")
@RestController
@RequestMapping("/p/allinpay/order")
@AllArgsConstructor
public class AllinpayOrderController {

    private final AllinpayService allinpayService;

    @GetMapping("/queryBalance")
    @Operation(summary = "查询账户余额")
    public ServerResponseEntity<QueryBalanceResp> queryBalance() {
        String bizUserId = this.getBizUserId(AuthUserContext.getUserId());
        QueryBalanceResp queryBalanceResp = allinpayService.queryBalance(bizUserId, AuthUserContext.getSysType());
        return ServerResponseEntity.success(queryBalanceResp);
    }

    @GetMapping("/queryInExpDetail")
    @Operation(summary = "查询账户收支明细")
    public ServerResponseEntity<IPage<InExpDetail>> queryInExpDetail(PageParam<InExpDetail> pageParam,
                                                                     QueryInExpDetailDTO queryInExpDetailDTO) {
        String bizUserId = this.getBizUserId(AuthUserContext.getUserId());
        IPage<InExpDetail> pageVO = allinpayService.queryInExpDetail(bizUserId, pageParam, queryInExpDetailDTO);
        return ServerResponseEntity.success(pageVO);
    }

    @PostMapping("/payConfirmByFrontSms")
    @Operation(summary = "确认支付[前台+短信验证码])", description = "通联支付申请后，前端再调用此接口获取通联页面链接")
    public ServerResponseEntity<String> payConfirmByFrontSms(@RequestBody PayBySmsDTO payBySmsDTO) {
        String bizUserId = this.getBizUserId(AuthUserContext.getUserId());
        String url = allinpayService.payBySms(bizUserId, payBySmsDTO);
        return ServerResponseEntity.success(url);
    }

    @PostMapping("/payConfirmByFrontPwd")
    @Operation(summary = "确认支付[前台+密码验证版]", description = "通联支付申请后，前端再调用此接口获取通联页面链接")
    public ServerResponseEntity<String> payConfirmByFrontPwd(@RequestBody PayByPwdDTO payByPwdDTO) {
        String bizUserId = this.getBizUserId(AuthUserContext.getUserId());
        String url = allinpayService.payByPwd(bizUserId, payByPwdDTO);
        return ServerResponseEntity.success(url);
    }

    @PostMapping("/payConfirmByBackSms")
    @Operation(summary = "确认支付[后台+短信验证码]", description = "通联支付申请后，后端再调用此接口获取通联页面链接")
    public ServerResponseEntity<PayByBackSmsResp> payConfirmByBackSms(@RequestBody PayByBackSmsDTO payByBackSmsDTO) {
        String bizUserId = this.getBizUserId(AuthUserContext.getUserId());
        PayByBackSmsResp resp = allinpayService.payByBackSms(bizUserId, payByBackSmsDTO);
        return ServerResponseEntity.success(resp);
    }

    @GetMapping("/resendPaySms")
    @Operation(summary = "重新发送支付短信验证码")
    public ServerResponseEntity<ResendPaySmsResp> resendPaySms(String bizOrderNo) {
        ResendPaySmsResp resp = allinpayService.resendPaySms(bizOrderNo);
        return ServerResponseEntity.success(resp);
    }

    @GetMapping("/getOrderStatus")
    @Operation(summary = "查询订单状态")
    public ServerResponseEntity<GetOrderStatusResp> getOrderStatus(String bizOrderNo) {
        String bizUserId = this.getBizUserId(AuthUserContext.getUserId());
        GetOrderStatusResp resp = allinpayService.getOrderStatus(bizUserId, bizOrderNo);
        return ServerResponseEntity.success(resp);
    }

    @GetMapping("/getAliAppPayUrl")
    @Operation(summary = "获取支付宝app支付url")
    public ServerResponseEntity<String> getAliAppPayUrl(AliAppPayUrlDTO aliAppPayUrlDTO) {
        String url = allinpayService.buildAliAppPayUrl(aliAppPayUrlDTO);
        return ServerResponseEntity.success(url);
    }

    /**
     * 获取通联平台用户id
     * @param userId 用户id
     * @return
     */
    private String getBizUserId(String userId) {
        return AllinpayConstant.USER + userId;
    }
}
